問題描述
無法將 NULL 值傳遞給數據庫 (Unable to pass NULL value to database)
我有一個 VB 應用程序。我有一個類似
Public Structure Deal
Public IsUser As Boolean?
End Structure
其他地方的屬性,我正在為其分配NULL
bject.IsUser = Nothing
現在我將其作為參數傳遞給SP。
AddParameter(SQLCommand, "@IsUser", SqlDbType.Bit, ParameterDirection.Input, obj.IsUser)
上面拋出錯誤 ‑
[InvalidOperationException: Nullable object must have a value.]
參考解法
方法 1:
Nothing
and DBNull
are not the same. Following should do the trick:
AddParameter(SQLCommand, "@IsUser", SqlDbType.Bit, ParameterDirection.Input, If(obj.IsUser Is Nothing, DBNull.Value, obj.IsUser))
方法 2:
you have to pass your variable as db null instead of nothing
bject.IsUser = DBNull.Value
(by user1926138、Alex B.、jamiedanq)